<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <style>
        .item img{
            width: 150px;
            height: 150px;
            padding: 5px;
        }
        .item{
            border: 1px solid #333;
            float: left;
            margin-right: 50px;
            margin-top: 50px;
        }
    </style>
</head>
<body>
    <div id="app"></div>
    <script>
        loadData()

        $(window).scroll(function(){
            if(isReachBottom()){
                var offset =$(".item").length;
                lodaData(offset)
            }
        })

        function http({
            offset = 0,
            success
        }){
            var url = `http:192.1684.18:3000/top/playlists?cat=华语&limit=20&offset=${offset}`
            $.ajax({
                url,
                success:res=>{
                    success(res)
                }
            })
        }

        function isReachBottom(){
            var scrollTop = $(window).scrollTop();
            var availHeight = $(window).height();
            var dh = $(document).height()
            return (availHeight+scrollTop == dh)?true:false;
        }

        function loadData(offset){
            http({
                offset,success:res=>{
                    var playlists = res.playlists;
                    playlists.forEach(item=>{
                        var template =`
                            <div class="item">
                                <img src=${item.coverImgUrl}>    
                            </div>
                        `
                        $("#app").append(template)
                    })
                }
            })
        }
    </script>
</body>
</html>